package longrise.phone.com.bjjt_henan.Utils;

import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.Utils.icomet.ChannelAllocator;
import longrise.phone.com.bjjt_henan.Utils.icomet.ICometCallback;
import longrise.phone.com.bjjt_henan.Utils.icomet.ICometClient;
import longrise.phone.com.bjjt_henan.Utils.icomet.ICometConf;
import longrise.phone.com.bjjt_henan.Utils.icomet.IConnCallback;

/**
 * 推送消息的管理类
 */
public class ICometManager {

    private ICometClient client;

    private IConnCallback connCallback;

    private ICometCallback cometCallback;

    private ChannelAllocator channelAllocator;

    public AtomicInteger cometCount = new AtomicInteger(0);// 连接计数

    private Timer timer;

    private int status = 0;// 连接状态

    public ICometManager(IConnCallback conn, ICometCallback comet,
                         ChannelAllocator channel) {
        this.connCallback = conn;
        this.cometCallback = comet;
        this.channelAllocator = channel;
        startTimer();
    }

    // 启动连接
    public void startIMConnect() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                connectIComet();
            }
        }).start();
    }

    private void connectIComet() {
        try {
            stop();

            status = 1;

            client = ICometClient.getInstance();
            ICometConf conf = new ICometConf();
            conf.host = URLConstant.HOST;
            conf.port = URLConstant.REC_PORT;
            conf.url = "pushlet/stream";
            conf.iConnCallback = connCallback;
            conf.iCometCallback = cometCallback;
            conf.channelAllocator = channelAllocator;
            client.prepare(conf);
            client.connect();
        } catch (Exception e) {
            stop();
            e.printStackTrace();
        }
    }

    private void startTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (status == 1) {
                    cometCount.incrementAndGet();
                    if (cometCount.get() > 3) {
                        // 45秒内无数据传递则重新连接
                        connectIComet();
                    }
                }
            }
        }, 1, 15 * 1000);
    }

    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void comet() {
        if (client != null) {
            client.comet();
        }
    }

    private void stop() {
        try {
            status = 0;
            if (client != null) {
                client.stopComet();
                client.stopConnect();
                Log.i("stop", "---stop success---");
            }
            client = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 关闭连接
    public void stopIMConnect() {
        stopTimer();
        new Thread(new Runnable() {
            @Override
            public void run() {
                stop();
            }
        }).start();
    }
}
